package tcp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class Chater implements Runnable {

	private Socket sk;

	public Chater(Socket sk) {
		super();
		this.sk = sk;
	}

	private void chat() throws Exception {
		OutputStream outputStream = sk.getOutputStream();
		InputStream in = sk.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String input = "Hey";
		while (true) {
			outputStream.write("What's your name?\n".getBytes());
			input = reader.readLine();
			if (input.equals("bye")) {
				outputStream.write("GoodBye~".getBytes());
				break;
			}
			outputStream.write(("Hello  " + input + ".\n").getBytes());
			outputStream.write(("finish!\n").getBytes());
		}
		outputStream.close();
		sk.close();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			chat();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
